<!--
 @description 招商管理-空白市场管理
 @fileName index.vue
 @author zengqiongying
 @created 2023-04-13 14:56:39
-->
<template>
  <div class="has-top-pt">
    <div class="ctm-content-head-fixed">
      <SearchInput
        v-model="searchValue"
        placeholder="输入市场名称"
        :show-action="false"
        @search="onSearch"
        @clear="onClear"
        @cancel="onSearch"
      />
    </div>

    <div class="customer-list">
      <BlankMarketList ref="list" :search-value="searchValue" />
    </div>
    <div class="ctm-content-foot">
      <qmp-button type="primary" round class="btn ctm-btn-main" @click="toAddPage">新增空白市场</qmp-button>
    </div>
  </div>
</template>

<script lang="ts">
  import { reactive, toRefs, getCurrentInstance, onActivated } from 'vue'
  import { useStore } from 'vuex'
  import SearchInput from '@/components/SearchInput/index.vue'
  import BlankMarketList from './components/List.vue'
  import { useRouter, onBeforeRouteLeave } from 'vue-router'

  export default {
    name: 'BlankMarket',
    components: { SearchInput, BlankMarketList },
    setup() {
      const $store = useStore()

      const { proxy, type }: any = getCurrentInstance()
      const datas = reactive({
        searchValue: '',
        listReLoad: false
      })
      const $router = useRouter()

      const onSearch = () => {
        // 搜索
      }
      const onClear = () => {
        // 搜索
      }

      const toAddPage = () => {
        // 新增空白市场
        $router.push({
          path: '/blankMarket/add'
        })
      }
      onBeforeRouteLeave((to, from) => {
        const instanceName = type?.name
        if (to.name === 'Index') {
          $store.commit('routerKeep/removeCache', instanceName)
        } else {
          $store.commit('routerKeep/resetCache', instanceName)
        }
      })

      proxy.$SimpleEvents.registerEvent('refreshCustomerChangeItem', () => {
        // // 新增
        // if (!id) {
        datas.listReLoad = true
        //   return
        // }
        // // 编辑
        // proxy.$refs.list.refreshItem(id)
      })

      onActivated(() => {
        // 缓存路由才生效
        if (datas.listReLoad) {
          proxy.$refs.list.resetPageData()
        }
        datas.listReLoad = false
      })
      return {
        ...toRefs(datas),
        onSearch,
        onClear,
        toAddPage
      }
    }
  }
</script>
<style lang="less" scoped>
  .customer-list {
    position: relative;
    padding: 0 14px 58px;
  }
</style>
